# -*- coding: utf-8 -*-

import math

def collision(first, second):
    return (first[0] < second[1]) and \
        (second[0] < first[1]) and \
        (first[2] < second[3]) and \
        (second[2] < first[3])
    
def distance(first, second):
    deltax = first[0]-second[0]
    deltay = first[1]-second[1]
    return math.hypot(deltax, deltay)

class GameObject(object):
    def __init__(self):
        #self.sprite.image.anchor_x = self.sprite.image.width/2
        #self.sprite.image.anchor_y = self.sprite.image.height/2
        self.rect = self.get_rect()

    def draw(self):
        self.sprite.draw()

    def update(self, dt):
        pass
    
    def get_rect(self):
        return (self.sprite.x-self.sprite.image.width/2,
            self.sprite.x+self.sprite.image.width/2, 
            self.sprite.y-self.sprite.image.height/2, 
            self.sprite.y+self.sprite.image.height/2)

    def keep_out_of(self, rect, lastrect):
        if lastrect[1] <= rect[0]:
            self.sprite.x = rect[0] - \
                self.sprite.image.width/2
        elif lastrect[0] >= rect[1]:
            self.sprite.x = rect[1] + \
                self.sprite.image.width/2
        elif lastrect[3] <= rect[2]:
            self.sprite.y = rect[2] - \
                self.sprite.image.height/2
        else:
            self.sprite.y = rect[3] + \
                self.sprite.image.height/2

    def die(self):
        self.sprite.delete()

    def be_hit(self, damage):
        pass

#it was worth a try, now remove
class Rect(object):
    def __init__(self, x, y, width, height):
        self.w = width
        self.h = height
        self.x = x
        self.y = y


    def __setitem__(self, key, value):
        if key == 0:
            self.left = value
        elif key == 1:
            self.right = value
        elif key == 2:
            self.bottom = value
        elif key == 3:
            self.top = value
        else:
            raise IndexError

    def __getitem__(self, key):
        if key == 0:
            return self.left
        elif key == 1:
            return self.right
        elif key == 2:
            return self.bottom
        elif key == 3:
            return self.top
        else:
            raise IndexError

    def __len__(self):
        return 4

    def __setattr__(self, name, value):
        if name == "x":
            self.__dict__["left"] = value
            self.__dict__["right"] = value + self.w
        elif name == "y":
            self.__dict__["bottom"] = value
            self.__dict__["top"] = value + self.h
        elif name == "left":
            self.x = value
        elif name == "right":
            self.x = value - self.w
        elif name == "bottom":
                    self.y = value
        elif name == "top":
                    self.y = value - self.h
        super(Rect, self).__setattr__(name, value)
    
    def __repr__(self):
        text= "<Rect: " + " ".join([str(self.left),str(self.right), 
           str(self.bottom), str(self.top)]) + ">"
        return text

    def collides(self, other):
        return (self.left < other.right) and \
           (other.left < self.right) and \
           (self.bottom < other.top) and \
           (other.bottom < self.top)

class World(object):
    player = None
    buildings = []
    enemies = []
    player_bullets = []
    enemy_bullets = []

    @staticmethod
    def add_enemies(*args):
        for enemy in args:
            self.enemies.append(enemy)

    @staticmethod
    def clear():
        player = None
        buildings = []
        enemies = []
        player_bullets = []
        enemy_bullets = []
            
